//
//  HomePageViewController.m
//  Mecca
//
//  Created by Nico Villanueva iOS Dev on 26/8/14.
//  Copyright (c) 2014 TapDash Heuristics. All rights reserved.
//

#import "HomePageViewController.h"
#import "UIManager.h"
#import "RotationTabViewController.h"

#import "Constant.h"

#import "PostClass.h"

#import "ParserClass.h"

#import "JsonClass.h"

#import "HomeData.h"

#import "ILTranslucentView.h"

#import "GlobalVariables.h"

#import "Artist.h"

#import "IIViewDeckController.h"

@interface HomePageViewController (){
    
    NSDictionary *json;
    NSArray *searchResults;
    
    BOOL varStat;
}

@property (strong, nonatomic) NSString *objectID;

@end

@implementation HomePageViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
        self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    }

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(somethingHappens:) name:@"notificationName" object:nil]; // Register Notification
    
    [self.navigationController.navigationBar setHidden:YES];

        self.actLoading.hidden = NO;
    
        self.actLoading.hidden = YES;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelNewsFeed" object:@"home"];
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"cancelFavorites" object:@"cancelFavorites"];

    self.receivedData = [NSMutableData data]; // Data
    NSURLRequest *request = [[PostClass sharedItems] getHomeData:[UIDevice currentDevice].identifierForVendor.UUIDString];
    _theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(tabsChooser:)];
    swipeRight.delegate = self;
    [self.view addGestureRecognizer:swipeRight];
    
}



-(void) somethingHappens:(NSNotification*) notification
{
    NSLog(@"notification cancelled = %@",[notification object]);
    
    [self.theConnection cancel];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)fetchedData:(NSData *)responseData {
    
 
}



#pragma mark - Button Actions

- (IBAction)btnAOTM:(id)sender {
    [self.navigationController pushViewController:[[UIManager sharedItems] artistOfTheMonthViewController] animated:YES];
}

- (IBAction)btnFeatSongs:(id)sender {
    [self.navigationController pushViewController:[[UIManager sharedItems] featuredSongsViewController] animated:YES];
}

- (IBAction)btnFeatArtist:(id)sender {
    [self.navigationController pushViewController:[[UIManager sharedItems] featuredArtistViewController] animated:YES];
}

- (IBAction)btnNewArtist:(id)sender {
    [self.navigationController pushViewController:[[UIManager sharedItems] newArtistViewController] animated:YES];
}

- (IBAction)btnNewSong:(id)sender {
    [self.navigationController pushViewController:[[UIManager sharedItems] newSongsViewController] animated:YES];
}


#pragma mark - Loading Image


-(void)setImageStringAOTM:(NSString *)strImage{
    
    NSString *strImg = [NSString stringWithFormat:@"http://192.185.93.102/~boomeran/backend-panel/%@",strImage];
    
    [NSThread detachNewThreadSelector:@selector(showImageAOTM:) toTarget:self withObject:strImg];
}

-(void)showImageAOTM:(NSString*)imageShow{
    
    @autoreleasepool {
        NSData *imageList = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageShow]];
        [self.imgArtistOfTheMonth setAlpha:0];
        [UIView animateWithDuration:1 animations:^{
            
            UIImage *image = [UIImage imageWithData:imageList];
            
            self.imgArtistOfTheMonth.image = image;
            [self.imgArtistOfTheMonth setAlpha:1];
            
        } completion:^(BOOL finished) {
        }];
    }
}

- (IBAction)btnAds:(id)sender {
    
    HomeData *homeData = [[ParserClass sharedItems] homeDataInterface][0];
    
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[homeData adsLink]]];
    
}

- (IBAction)btnTest:(id)sender {
    
    [[UIManager sharedItems]closeLeft];
    
}

- (IBAction)userProfileAction:(id)sender {
    
    [[UIManager sharedItems]openLeft];
    
}

- (IBAction)tabsChooser:(id)sender {
    
    NSLog(@"Swipe Chooser");
    [[UIManager sharedItems]openRight];
}


#pragma mark - URL Connection Delegate

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [_receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"Error data = %@", error);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    
    if (connection == self.theConnection) {
        
        [_receivedData appendData:data];
        
        //  NSLog(@"data parse = %@", _receivedData);
        
    }
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
    
    if (connection == self.theConnection) {
        
        if (!(_receivedData == nil)) {
            
            NSLog(@"Count Confirmed");
            
            json = [JsonClass jsonParser:_receivedData];
            
            [[ParserClass sharedItems] gethomeDataInterface:json];
            
            //[self.tableView reloadData];
            
            NSLog(@"Home Details = %@", json);
            
            NSLog(@"Datas = %i",[[[ParserClass sharedItems] homeDataInterface] count]);
            HomeData *homeData = [[ParserClass sharedItems] homeDataInterface][0];
            
            self.lblAOTMName.text = [homeData artistName];
            
            [self setImageStringAOTM:[homeData artistPicture]];
            
            
        }

        
        NSLog(@"Finish");
    }
    
}

@end
